package com.estar.assistsurvy.adapter;

import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.MessageList;
import com.estar.assistsurvy.entity.PlatformMessage;
import com.estar.assistsurvy.utils.TimeUtils;
import com.google.gson.Gson;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MessageListViewAdapter extends BaseAdapter {

	private List<MessageList> messageList;
	private LayoutInflater inflater;
	private Context context;
	private Gson gson;
	public MessageListViewAdapter(List<MessageList> messageList, Context context) {
		super();
		this.messageList = messageList;
		this.context=context;
		inflater=LayoutInflater.from(context);
		gson=new Gson();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return messageList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return messageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView==null){
			convertView=inflater.inflate(R.layout.listview_messages, null);
			holder=new ViewHolder();
			holder.showMsg=(TextView) convertView.findViewById(R.id.showMsg);
			holder.showTime=(TextView) convertView.findViewById(R.id.showTime);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		MessageList message=messageList.get(position);
		holder.showTime.setText(TimeUtils.EnglishToChinese(message.getCreateTime()));
		PlatformMessage mes=new PlatformMessage();
		mes=gson.fromJson(message.getMessage(), PlatformMessage.class);
		holder.showMsg.setText(mes.getMessage());
		if(message.getIsRead().equals("0")){
			holder.showTime.setTextColor(context.getResources().getColor(R.color.colorBlack));
			holder.showMsg.setTextColor(context.getResources().getColor(R.color.colorBlack));
		}else if(message.getIsRead().equals("1")){
			holder.showTime.setTextColor(context.getResources().getColor(R.color.colorTextGrey));
			holder.showMsg.setTextColor(context.getResources().getColor(R.color.colorTextGrey));
		}
		
		if(clickItem==position){
			holder.showTime.setTextColor(context.getResources().getColor(R.color.colorBlack));
			holder.showMsg.setTextColor(context.getResources().getColor(R.color.colorBlack));
		}
		
		return convertView;
	}
	
	private class ViewHolder{
		private TextView showTime,showMsg;
	}
	
	private int clickItem=-1;
	public void setClickItem(int clickItem){
		this.clickItem=clickItem;
	}

}
